package com.entities;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.dstats.Constants;
import com.fetch.Fetch;

public class SteamUser {
	private static final String url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/";
	private static final long convertTo64Bits =  Long.valueOf("76561197960265728");
	private long steamId;
	private String personaname;
	private long lastlogoff;
	private String profileurl;
	private String avatarUrl;
	private int communityvVsibilityState;
	private int personastate;
	private String realname;
	private String primaryclanid;
	private long timecreated;
	private String loccountrycode;
	private String locstatecode;
	private String loccityid;
	
	private static final String  STEAMID = "steamid";
	private static final String  PERSONANAME = "personaname";
	private static final String  LASTLOGOFF = "lastlogoff";
	private static final String  PROFILEURL = "profileurl";
	private static final String  AVATARURL = "avatar";
	private static final String  COMMUNITYVVSIBILITYSTATE = "communityvisibilitystate";
	private static final String  PERSONASTATE = "personastate";
	private static final String  REALNAME = "realname";
	private static final String  PRIMARYCLANID = "primaryclanid";
	private static final String  TIMECREATED = "timecreated";
	private static final String  LOCCOUNTRYCODE = "loccountrycode";
	private static final String  LOCSTATECODE = "locstatecode";
	private static final String  LOCCITYID = "loccityid";
	
	private static HashMap<String, SteamUser> hm = new HashMap<String, SteamUser>();
	public long getSteamId() {
		return steamId;
	}


	public String getPersonaname() {
		return personaname;
	}


	public long getLastlogoff() {
		return lastlogoff;
	}


	public String getProfileurl() {
		return profileurl;
	}


	public String getAvatarUrl() {
		return avatarUrl;
	}


	public int getCommunityvVsibilityState() {
		return communityvVsibilityState;
	}


	public int getPersonastate() {
		return personastate;
	}


	public String getRealname() {
		return realname;
	}


	public String getPrimaryclanid() {
		return primaryclanid;
	}


	public long getTimecreated() {
		return timecreated;
	}


	public String getLoccountrycode() {
		return loccountrycode;
	}


	public String getLocstatecode() {
		return locstatecode;
	}


	public String getLoccityid() {
		return loccityid;
	}

	
	public SteamUser(JSONObject json){
		
		long steamid;
		try {
			steamid = Long.valueOf(json.getString(STEAMID));
		} catch (JSONException e) {
			steamid=-1;
		}
		String personaname;
		try {
			personaname = json.getString(PERSONANAME);
		} catch (JSONException e) {
			personaname = "Anonymous";
		}
		long lastlogoff;
		try {
			lastlogoff = Long.valueOf(json.getString(LASTLOGOFF));
		} catch (JSONException e) {
			lastlogoff = -1;
		}
		String profileurl;
		try {
			profileurl = json.getString(PROFILEURL);
		} catch (JSONException e) {
			profileurl = "";
		}
		String avatarUrl;
		try {
			avatarUrl = json.getString(AVATARURL);
		} catch (JSONException e) {
			avatarUrl = "";
		}
		int communityvVsibilityState;
		try {
			communityvVsibilityState = json.getInt(COMMUNITYVVSIBILITYSTATE);
		} catch (JSONException e) {
			communityvVsibilityState = -1;
		}
		int personastate;
		try {
			personastate = json.getInt(PERSONASTATE);
		} catch (JSONException e) {
			personastate = -1;
		}
		String realname;
		try {
			realname = json.getString(REALNAME);
		} catch (JSONException e) {
			realname = "";
		}
		String primaryclanid;
		try {
			primaryclanid = json.getString(PRIMARYCLANID);
		} catch (JSONException e) {
			primaryclanid = "";
		}
		long timecreated;
		try {
			timecreated = Long.valueOf(json.getString(TIMECREATED));
		} catch (JSONException e) {
			timecreated = -1;
		}
		String loccountrycode;
		try {
			loccountrycode = json.getString(LOCCOUNTRYCODE);
		} catch (JSONException e) {
			loccountrycode = "";
		}
		String locstatecode;
		try {
			locstatecode = json.getString(LOCSTATECODE);
		} catch (JSONException e) {
			locstatecode = "";
		}
		String loccityid;
		try {
			loccityid = json.getString(LOCCITYID);
		} catch (JSONException e) {
			loccityid = "";
		}
		
		this.steamId = steamid;
		this.personaname = personaname;
		this.lastlogoff = lastlogoff;
		this.profileurl = profileurl;
		this.avatarUrl = avatarUrl;
		this.communityvVsibilityState = communityvVsibilityState;
		this.personastate = personastate;
		this.realname = realname;
		this.primaryclanid = primaryclanid;
		this.timecreated = timecreated;
		this.loccountrycode = loccountrycode;
		this.locstatecode = locstatecode;
		this.loccityid = loccityid;
		
	}

	
	public static HashMap<String, SteamUser> getSteamUserOfJsonObj(List<Player> pList){
		List<Long> userIds = new ArrayList<Long>();
		HashMap<String, SteamUser> hmpl = new HashMap<String, SteamUser>();
		for(Player p: pList){
			String stringAccId = String.valueOf(p.getAccount_id());
			if(hm.get(stringAccId)==null){
			userIds.add(p.getAccount_id());
			} else {
				hmpl.put(stringAccId, hm.get(p.getAccount_id()));
			}
		}
		
		try {
			JSONArray json = new FetchSteamUser(userIds).execute().get().getJSONObject("response").getJSONArray("players");
			for(int cont = 0; cont<json.length();cont++){
				JSONObject j = json.getJSONObject(cont);				
				SteamUser steamUser = new SteamUser(j);
				hmpl.put(String.valueOf(steamUser.getSteamId() - convertTo64Bits), steamUser);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (JSONException e1) {
			e1.printStackTrace();
		} 
		return hmpl;

	}
	
	static class FetchSteamUser extends Fetch{

		private String userId;
		private List<Long> userIds;
		
		public FetchSteamUser(long userId) {
		 this.userId = String.valueOf(convertTo64Bits + userId);
		}
		
		public FetchSteamUser(String userId) {
			 this.userId = convertTo64Bits + String.valueOf(userId);
			}
		
		public FetchSteamUser(List<Long> userIds) {
			 this.userIds =userIds;
			}
			
		private String appendedString(){
			StringBuilder sb = new StringBuilder();
			for(long l: userIds){
				sb.append(String.valueOf(l+convertTo64Bits) + ";");
			}
			return sb.toString();
		}
		
		@Override
		protected List<NameValuePair> getParams() {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair(Constants.KEY, Constants.apiKey));
			if(userIds==null){
				params.add(new BasicNameValuePair(Constants.STEAMIDS, userId));
			} else {
				params.add(new BasicNameValuePair(Constants.STEAMIDS, appendedString()));
			}
			return params;
		}

		@Override
		protected String getUrl() {
			return url;
		}
		
	}
	
}
